package com.balysv.materialripple.demo.slice;

import com.balysv.materialripple.MaterialRippleLayout;
import com.balysv.materialripple.demo.ResourceTable;
import com.balysv.materialripple.demo.util.CustomToolbar;

import ohos.aafwk.ability.AbilitySlice;
import ohos.aafwk.content.Intent;
import ohos.aafwk.content.Operation;
import ohos.agp.components.Button;
import ohos.agp.components.Component;
import ohos.agp.components.DirectionalLayout;
import ohos.agp.utils.Color;
import ohos.agp.utils.LayoutAlignment;
import ohos.agp.window.dialog.ToastDialog;
import ohos.eventhandler.EventHandler;
import ohos.eventhandler.EventRunner;

public class MainAbilitySlice extends AbilitySlice {
    private Button mBtn;
    private DelayLongRunAnimator delayLongRunAnimator = new DelayLongRunAnimator();
    private EventHandler handler;
    private HandlerRunClick mHandlerRunClick = new HandlerRunClick();
    private CustomToolbar customToolbar;

    @Override
    public void onStart(Intent intent) {
        super.onStart(intent);
        super.setUIContent(ResourceTable.Layout_ability_main);
        handler = new EventHandler(EventRunner.getMainEventRunner());
        customToolbar = (CustomToolbar) findComponentById(ResourceTable.Id_toolbar_view);
        Button button = (Button) findComponentById(ResourceTable.Id_ripple_view);
        mBtn = (Button) findComponentById(ResourceTable.Id_btn_click);
        button.setClickedListener(new Component.ClickedListener() {
            @Override
            public void onClick(Component component) {
                handler.postTask(mHandlerRunClick, 400);
            }
        });
        button.setLongClickedListener(new Component.LongClickedListener() {
            @Override
            public void onLongClicked(Component component) {
                handler.postTask(delayLongRunAnimator, 1600);
            }
        });

        MaterialRippleLayout.on(mBtn)
                .rippleColor(Color.getIntColor("#00000000"))
                .rippleAlpha(0.2f)
                .rippleOverlay(true)
                .rippleDiameterDp(30)
                .create();

        mBtn.setClickedListener(new Component.ClickedListener() {
            @Override
            public void onClick(Component component) {
                handler.postTask(mHandlerRunClick, 400);
            }
        });

        mBtn.setLongClickedListener(new Component.LongClickedListener() {
            @Override
            public void onLongClicked(Component component) {
                handler.postTask(delayLongRunAnimator, 1600);
            }
        });

        customToolbar.getTextListView().setText("RECYCLERVIEW");
        customToolbar.getTextListView().setClickedListener(new Component.ClickedListener() {
            @Override
            public void onClick(Component component) {
                Intent intent = new Intent();
                Operation operation = new Intent.OperationBuilder()
                        .withDeviceId("")
                        .withBundleName("com.balysv.materialripple.demo")
                        .withAbilityName("com.balysv.materialripple.demo.RecyclerViewAbility")
                        .build();
                intent.setOperation(operation);
                startAbility(intent);
                terminateAbility();
            }
        });
        customToolbar.getTextRecyclerView().setText("LISTVIEW");
        customToolbar.getTextRecyclerView().setClickedListener(new Component.ClickedListener() {
            @Override
            public void onClick(Component component) {
                Intent intent = new Intent();
                Operation operation = new Intent.OperationBuilder()
                        .withDeviceId("")
                        .withBundleName("com.balysv.materialripple.demo")
                        .withAbilityName("com.balysv.materialripple.demo.ListViewAbility")
                        .build();
                intent.setOperation(operation);
                startAbility(intent);
                terminateAbility();
            }
        });
    }

    private class HandlerRunClick implements Runnable {
        @Override
        public void run() {
            new ToastDialog(MainAbilitySlice.this).setContentText("Short click").setSize(DirectionalLayout.LayoutConfig.MATCH_CONTENT, DirectionalLayout.LayoutConfig.MATCH_CONTENT)
                    .setAlignment(LayoutAlignment.BOTTOM | LayoutAlignment.HORIZONTAL_CENTER)
                    .setOffset(0, 60).show();
        }
    }

    private class DelayLongRunAnimator implements Runnable {
        @Override
        public void run() {
            new ToastDialog(MainAbilitySlice.this).setContentText("Long click not consumed").setSize(DirectionalLayout.LayoutConfig.MATCH_CONTENT, DirectionalLayout.LayoutConfig.MATCH_CONTENT)
                    .setAlignment(LayoutAlignment.BOTTOM | LayoutAlignment.HORIZONTAL_CENTER)
                    .setOffset(0, 60).show();
        }
    }
}
